from base import *

def dims(h, w): # Zero means no bound
    return [h, w]

class image(tag):
    def __init__(self, src, dim=[0, 0], childs=[]):
        tag.__init__(self, childs)
        self.source = src
        self.dims = dim
        self.addTagOpt("src", self.source)
        self.addTagOpt("height", self.dims[0])
        self.addTagOpt("width", self.dims[1])
                

from data import table
class array(table):
    def __init__(self, srcs,
                 img_dims=[0,0],
                 tab_dims=[0, 4],
                 img_class=image,
                 childs=[]):

        table.__init__(self)
        self.sources = srcs
        self.image_dims = img_dims
        self.table_dims = tab_dims
        self.image_class = img_class
        self.fromList(self.makeList())

    def pullRow(self):
        r = []
        if self.table_dims[1] == 0:
            return self.sources

        for i in range(self.table_dims[1]):
            if self.sources == []:
                return r
            r.append(self.image_class(self.sources.pop(),
                                      self.image_dims))
        return r

    def makeList(self):
        l = []
        while self.table_dims[0] == 0 or len(l) < self.table_dims[0]:
            if self.sources == []:
                return l
            l.append(self.pullRow())
        return l
        
    def toHTML(self):
        return (("<table%s>" % self.getTagOpts()) +
                self.childHTML() + "</table>")
